/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.campaign.entity.CampaignExtend
   Module Description   :

   Date Created      : 2010/6/28
   Original Author   : jeffma
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.campaign.entity;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.GenericGenerator;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author jeffma
 * 
 */
@Entity
@Table(name = "EC_CAMPAIGN_EXT")
public class CampaignExtend implements Serializable {

	/** serialVersionUID */
	private static final long serialVersionUID = 558215156203288655L;

	public static final BigDecimal MAX_AMOUNT = new BigDecimal("999999999.9");
	public static final int MAX_QTY = 99999;

	@Id
	@GeneratedValue(generator = "gen_uuid")
	@GenericGenerator(name = "gen_uuid", strategy = "uuid")
	@Column(name = "OBJ_UUID")
	private String uuid;
	/** compaign */
	@ManyToOne
	@JoinColumn(name = "PARENT_UUID")
	private Campaign campaign;
	/**
	 * 最低订单金额 Minimal Order Amount, 即订单需满足此金额才能适用此活动优惠
	 */
	@Column(name = "MIN_ORDER_AMOUNT", columnDefinition = "numeric(19,2)")
	private BigDecimal minOrderAmount = BigDecimal.ZERO;
	/**
	 * 最高订单金额 Minimal Order Amount, 即订单需满足此金额才能适用此活动优惠
	 */
	@Column(name = "MAX_ORDER_AMOUNT", columnDefinition = "numeric(19,2)")
	private BigDecimal maxOrderAmount = new BigDecimal("999999999.9");
	/** maxQty */
	@Column(name = "MAX_QTY", columnDefinition = "numeric(5)")
	private int maxQty = MAX_QTY;
	/** minQty */
	@Column(name = "MIN_QTY", columnDefinition = "numeric(5)")
	private int minQty = 0;

	/**
	 * campaignTypeExtension,
	 * 
	 * Type C01 放置折扣金額如 100(為正整數), Type C02 放置折扣比例如 80(%)(為正整數, 且介於 1~99), Type C03 放置ProdId;SpecId. Type
	 * M01積分發送，每2元發送1點積分(為正整數)，可設定封頂上限最多幾分
	 */
	@Column(name = "CAMPAIGN_TYPE_EXT", length = 100)
	private String campaignTypeExtension;

	/**
	 * 积分发送資料
	 */
	@Column(name = "JSON_DETAIL", length = 200)
	private String jsonDetail;

	/**
	 * 是否封顶
	 */
	@Transient
	private boolean isUpperLimit = false;

	/**
	 * 积分上限最高几分
	 */
	@Transient
	private String bonusLimit;

	/** default constructor */
	public CampaignExtend() {
		super();
	}

	/** default constructor */
	public CampaignExtend(BigDecimal minOrderAmount, BigDecimal maxOrderAmount, int maxQty, int minQty,
			String campaignTypeExtension) {
		super();
		this.minOrderAmount = minOrderAmount;
		this.maxOrderAmount = maxOrderAmount;
		this.maxQty = maxQty;
		this.minQty = minQty;
		this.campaignTypeExtension = campaignTypeExtension;
	}

	/** default constructor */
	public CampaignExtend(BigDecimal minOrderAmount, BigDecimal maxOrderAmount, String campaignTypeExtension) {
		super();
		this.minOrderAmount = minOrderAmount;
		this.maxOrderAmount = maxOrderAmount;
		this.campaignTypeExtension = campaignTypeExtension;
	}

	/** default constructor */
	public CampaignExtend(BigDecimal minOrderAmount, String campaignTypeExtension) {
		super();
		this.minOrderAmount = minOrderAmount;
		this.campaignTypeExtension = campaignTypeExtension;
	}

	/** default constructor */
	public CampaignExtend(String campaignTypeExtension) {
		super();
		this.campaignTypeExtension = campaignTypeExtension;
	}

	/**
	 * @return the uuid
	 */
	public String getUuid() {
		return uuid;
	}

	/**
	 * @param uuid the uuid to set
	 */
	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	/**
	 * @return the campaign
	 */
	public Campaign getCampaign() {
		return campaign;
	}

	/**
	 * @param compaign the campaign to set
	 */
	public void setCampaign(Campaign campaign) {
		this.campaign = campaign;
	}

	/**
	 * @return the minOrderAmount
	 */
	public BigDecimal getMinOrderAmount() {
		return minOrderAmount;
	}

	/**
	 * @param minOrderAmount the minOrderAmount to set
	 */
	public void setMinOrderAmount(BigDecimal minOrderAmount) {
		this.minOrderAmount = minOrderAmount;
	}

	/**
	 * @return the maxOrderAmount
	 */
	public BigDecimal getMaxOrderAmount() {
		return maxOrderAmount;
	}

	/**
	 * @param maxOrderAmount the maxOrderAmount to set
	 */
	public void setMaxOrderAmount(BigDecimal maxOrderAmount) {
		this.maxOrderAmount = maxOrderAmount;
	}

	/**
	 * @return the maxQty
	 */
	public int getMaxQty() {
		return maxQty;
	}

	/**
	 * @param maxQty the maxQty to set
	 */
	public void setMaxQty(int maxQty) {
		this.maxQty = maxQty;
	}

	/**
	 * @return the minQty
	 */
	public int getMinQty() {
		return minQty;
	}

	/**
	 * @param minQty the minQty to set
	 */
	public void setMinQty(int minQty) {
		this.minQty = minQty;
	}

	/**
	 * @return the campaignTypeExtension
	 */
	public String getCampaignTypeExtension() {
		return campaignTypeExtension;
	}

	/**
	 * @param campaignTypeExtension the campaignTypeExtension to set
	 */
	public void setCampaignTypeExtension(String campaignTypeExtension) {
		this.campaignTypeExtension = campaignTypeExtension;
	}

	public String getJsonDetail() {
		try {
			if (StringUtils.isNotBlank(jsonDetail)) {
				JSONObject jsonObj = new JSONObject(jsonDetail);
				if (null != jsonObj) {
					if (Campaign.Type.M01.getCode().equals(campaign.getType().getCode())) {
						if (isUpperLimit == false && StringUtils.isBlank(bonusLimit)) {
							if (StringUtils.equals("false", jsonObj.getString("isUpperLimit"))) {
								setUpperLimit(false);
							} else {
								setUpperLimit(true);
							}
							setBonusLimit(jsonObj.getString("bonusLimit"));
						}
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return jsonDetail;
	}

	public void setJsonDetail(String jsonDetail) {
		this.jsonDetail = jsonDetail;
	}

	public boolean isUpperLimit() {
		getJsonDetail();
		return isUpperLimit;
	}

	public String getBonusLimit() {
		getJsonDetail();
		return bonusLimit;
	}

	public void setUpperLimit(boolean isUpperLimit) {
		this.isUpperLimit = isUpperLimit;
	}

	public void setBonusLimit(String bonusLimit) {
		this.bonusLimit = bonusLimit;
	}
}